package com.jeegod.ribbon.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.web.client.RestTemplate;

public class HelloServiceCommand extends HystrixCommand<String> {

    private RestTemplate restTemplate;

    public HelloServiceCommand(String commandGroupKey, RestTemplate restTemplate){
        //根据commandGroupKey进行线程隔离
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
    }
    @Override
    protected String run() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return restTemplate.getForEntity("http://HELLO-SERVICE1/hello", String.class).getBody();
    }
    @Override
    protected String getFallback(){
        return "error";
    }

    @Override
    protected String getCacheKey(){
        //一般动态的取缓存，比如userId，这里为了做实验写死了
        return "hello";
    }
}
